<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/16 11:27
 * @version
 */
namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\PushMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class PushController extends Controller
{
    public function search(Request $request)
    {
        return view('main::push_message', \Request::all());
    }

    public function doSearch(Request $request)
    {
        $data = [];
        $limit  = $request->input('limit', 10);
        $offset = $request->input('offset', 0);
        $datetime = $request->input('datetime');
        $query = PushMessage::query();
        if ($datetime && isset($datetime[0]) && isset($datetime[1])) {
            $query->whereBetween('created_at', $datetime);
        }
        $data['total'] = $query->count();
        $res           = $query->take($limit)->skip($offset)->orderBy('id', 'desc')->get();

        $data['items'] = [];
        foreach ($res as $item) {
            $_item = $item->toArray();

            $data['items'][] = $_item;
        }
        return success($data);
    }

    public function send(Request $request)
    {
        $id = $request->input('id');
        $info = PushMessage::find($id);
        $info->status = 1;
        $info->save();
        return success();
    }

    public function add(Request $request)
    {
        $title = $request->input('title');
        $body  = $request->input('body');
        $device = $request->input('device');
        $type  = $request->input('type');
        $collect = $request->input('collect');
        $redirect = $request->input('redirect');
        $vals = $request->input('vals');
        if($type=='collect'){
            $push_target_value = $collect;
        }else{
            $push_target_value = 'all';
        }
        $params = [
            'redirect'=>$redirect,'vals'=>$vals
        ];
        $data = [
            'title'=>$title,'body'=>$body,'push_target'=>$type,'push_target_value'=>$push_target_value,
            'push_device_type'=>$device,'params'=>json_encode($params,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_SLASHES)
        ];
        PushMessage::create($data);
        return success();
    }

    public function update(Request $request)
    {
        return success();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        $info = PushMessage::find($id);
        $info->delete();
        return success();
    }

    /**
     * 拨打电话
     *
     * @author yls
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function call(Request $request)
    {
        $mobileEn = $request->post('mobile_en', '');

        if (empty($mobileEn)) {
            return fail(400, '手机号码不存在');
        }

        $mobileEn = decrypt($mobileEn);

        if (!$mobileEn || strlen($mobileEn) != 11) {
            return fail(400, '手机号码错误'.$mobileEn);
        }

        $res = $this->hollycrmCall($mobileEn);
        return $res && 200 == $res ? success($res) : fail(400, '拨号失败'.$res);
    }
}